from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_jwt.serializers import jwt_decode_handler

from xyz.models import Question


class PutReplyquestionuseful(APIView):

    def put(self, request, id):

        try:

            token = self.request.auth

            token_user = jwt_decode_handler(token)

            user_id = token_user['user_id']

        except Exception as e:

            return Response(status=404)

        count = Question.objects.get(user_id=user_id, id=id)

        count.useful_count += 1

        count.save()

        return Response({'messgae': '问题有用',
                         'success': True})

class PutReplyquestionunuseful(APIView):


    def put(self, request, id):

        try:

            token = self.request.auth

            token_user = jwt_decode_handler(token)

            user_id = token_user['user_id']

        except Exception as e:

            return Response(status=404)

        count = Question.objects.get(user_id=user_id, id=id)

        count.useful_count -= 1

        count.unuseful_count += 1

        count.save()

        return Response({'messgae': '问题没用',
                          'success': True})